package com.example.h1909aapp.model;


//网络框架,Rxjava+Retrofit+okhttp
//okhttp:日志拦截器:4行; 缓存
//

import com.example.h1909aapp.base.BaseModel;
import com.example.h1909aapp.bean.NaviBean;
import com.example.h1909aapp.net.HttpUtil;
import com.example.h1909aapp.net.ResultCallBack;
import com.example.h1909aapp.net.ResultSubscriber;
import com.example.h1909aapp.net.RxUtils;

public class MainModel extends BaseModel {
    public void getData(final ResultCallBack<NaviBean> callBack) {
        //Disposable.dispose()切断观察者和被观察者的连接,以及结束网络请求
        //ResourceSubscriber 是Disposable的子类
        /*ResourceSubscriber<Bean> resourceSubscriber = new Retrofit.Builder()
                .baseUrl(ApiService.sUrl)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//支持rxjava
                .addConverterFactory(GsonConverterFactory.create())//支持数据解析工厂
                .build()
                .create(ApiService.class)
                .getData2()
                .subscribeOn(Schedulers.io())//被观察者运行的线程
                .observeOn(AndroidSchedulers.mainThread())//观察者运行的主线程
                .subscribeWith(new ResourceSubscriber<Bean>() {
                    @Override
                    public void onNext(Bean bean) {
                        callBack.onSuccess(bean);
                    }

                    @Override
                    public void onError(Throwable t) {
                        callBack.onFail(t.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

        //resourceSubscriber.dispose();
        addDisposable(resourceSubscriber);*/


        /*addDisposable(
                HttpUtil.getInstance()
                        .getWanService()
                        .getNavi()
                   *//*     .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())*//*
                        .compose(RxUtils.<NaviBean>rxSchedulerHelper())//切换观察者和被观察者的线程
                        .subscribeWith(new ResourceSubscriber<NaviBean>() {
                            @Override
                            public void onNext(NaviBean naviBean) {

                            }

                            @Override
                            public void onError(Throwable t) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        })
        );*/

        addDisposable(
                HttpUtil.getInstance()
                        .getWanService()
                        .getNavi()
                        .compose(RxUtils.<NaviBean>rxSchedulerHelper())//切换观察者和被观察者的线程
                        .subscribeWith(new ResultSubscriber<NaviBean>() {
                            @Override
                            public void onNext(NaviBean naviBean) {
                                callBack.onSuccess(naviBean);
                            }
                        })
        );
    }
}
